import { defineStore, storeToRefs } from "pinia";
import { STORE_PREFIX } from "@/utils/constant";
import { usePremsStore } from "@/store/modules/prems";

export const useManagePageStore = defineStore(`${STORE_PREFIX}manage_page_store`, () => {
  const premsStore = usePremsStore();
  const {
    isRoomAdmin,
    isMenuUser,
    isMenuReport,
    isMenuBackwater,
    isMenuMonitor,
    isMenuOdds,
    isMenuFlyalone,
    isMenuOpen,
    isMenuActivity
  } = storeToRefs(premsStore);

  const defaultList = () => {
    return [
      {
        label: "用户管理",
        key: "user",
        icon: "manage-user",
        path: "",
        show: isMenuUser.value,
        children: [
          { name: "用户列表", path: "/admin/user/member" },
          { name: "代理列表", path: "/admin/user/agent" },
          { name: "机器人列表", path: "/admin/user/robot" },
          { name: "子账号列表", path: "/admin/user/sub" }
        ],
        actives: []
      },
      {
        label: "报表",
        key: "report",
        icon: "manage-report",
        path: "",
        show: isMenuReport.value,
        children: [
          { name: "总报表", path: "/admin/report/total" },
          { name: "用户报表", path: "/admin/report/user" },
          { name: "分类报表", path: "/admin/report/classify" },
          { name: "未结报表", path: "/admin/report/unsettled" },
          { name: "返佣报表", path: "/admin/report/rebate" },
          { name: "红包报表", path: "/admin/report/redpack" },
          { name: "回水报表", path: "/admin/report/backwater" },
          { name: "告警报表", path: "/admin/report/notice" },
          { name: "日志报表", path: "/admin/report/log" }
        ],
        actives: []
      },
      {
        label: "回水管理",
        key: "backwater",
        icon: "manage-report",
        path: "",
        show: isMenuBackwater.value,
        children: [
          { name: "手动回水", path: "/admin/backwater/manual" },
          { name: "回水报表", path: "/admin/backwater/report" },
          { name: "回水设置", path: "/admin/backwater/setting" }
        ],
        actives: []
      },
      {
        label: "现场监控",
        key: "monitor",
        icon: "manage-monitor",
        path: "/admin/monitor",
        show: isMenuMonitor.value
      },
      {
        label: "赔率限额",
        key: "odds",
        icon: "manage-odds",
        path: "/admin/odds",
        show: isMenuOdds.value
      },
      {
        label: "打盘报表",
        key: "flyalone",
        icon: "manage-flyalone",
        path: "/admin/flyalone",
        show: isMenuFlyalone.value
      },
      {
        label: "开奖结果",
        key: "openresult",
        icon: "manage-openresult",
        path: "/admin/openresult",
        show: isMenuOpen.value
      },
      {
        label: "发红包",
        key: "send-red",
        icon: "manage-red",
        path: "/admin/main/send-red",
        show: isMenuActivity.value
      },
      {
        label: "二次验证",
        key: "authenticator",
        icon: "manage-authenticator",
        path: "/admin/main/authenticator",
        show: true
      },
      {
        label: "反馈",
        key: "feedback",
        icon: "manage-feedback",
        path: "/admin/main/feedback",
        show: true
      },
      {
        label: "H5/APP下载",
        key: "appdown",
        icon: "manage-appdown",
        path: "/admin/main/appdown",
        show: true
      }
    ];
  };
  const list = ref<ManageMenuItem[]>(defaultList());

  const scrollTop = ref(0);
  const resetList = () => {
    list.value.forEach(item => {
      item.actives = [];
    });
    setTimeout(() => {
      scrollTop.value = 0;
    }, 500);
  };
  watch(
    () => [
      isRoomAdmin.value,
      isMenuUser.value,
      isMenuReport.value,
      isMenuBackwater.value,
      isMenuMonitor.value,
      isMenuOdds.value,
      isMenuFlyalone.value,
      isMenuOpen.value,
      isMenuActivity.value
    ],
    () => {
      list.value = defaultList();
    }
  );

  return {
    list,
    scrollTop,
    resetList
  };
});
